<?php

namespace app\api\controller;

use app\api\controller\Auth;
use app\api\model\Token;
use app\api\model\User as userModel;
use app\common\controller\ApiController;
use app\Request;
use Icharle\Wxtool\Wxtool;
use think\App;
use think\facade\Db;
use think\facade\Log;

class User extends ApiController
{
    protected $noNeedLogin = ['wx_login','getopenid','getphone'];
    /**
     * 登录
     * @return string
     * @throws \Exception
     */
    public function wx_login(userModel $user)
    {
        $param = $this->request->param();
       
        $wx = new Wxtool();
        if (empty($param['code'])) {
            $this->error("参数有误");
        }
        // if (empty($param['code']) || empty($param['encryptedData']) || empty($param['iv'])) {
        //     $this->error("参数有误");
        // }
        $code = $param['code'];
        // $encryptedData = urldecode($param['encryptedData']);
        // $iv = urldecode($param['iv']);
        $res = $wx->GetSessionKey($code);

        $nickName = nickname();
        $avatarUrl = "https://lsjiazhen.yquewx.com/upload/logouser.jpg";

        $openId = $res['openid'];
      //  $unionId = empty($res['unionid']) ? '' : $res['unionid'];
        $isExit = $user->where('wx_openid', $openId)->find();
        //'unionId' => $unionId,   "nickname" => $nickName, 
        $saveData = ['update_time' => time(), 'avatar' => $avatarUrl];
        if (!empty($isExit)) {
            $uid = $isExit['id'];
            $ret = $user->where('id', $uid)->update($saveData);
            $gender = $isExit["gender"];
            $nickName = $isExit["nickname"];
        } else {
            $user = new userModel();
            $saveData['wx_openid'] = $openId;
            $saveData['nickname'] = $nickName;
            $ret = $user->save($saveData);
            $uid = $user->id;
            $gender = $user->gender;
        }  
        if ($ret) {
            $token = Auth::makeToken();
            $userToken = new Token();
            $userToken->setToken($token, $uid, $this->method);
            $user_info = ['nickname' => $nickName, 'avatar' => $avatarUrl,"uid"=>$uid,"gender"=>$gender];
            $this->success('登录成功', ['token' => $token, 'method' => $this->method, 'user_info' => $user_info]);
        } else {
            $this->error('登陆失败,请稍后重试001');
        }
    }


    public function getopenid(){
        $param = $this->request->param();
        $wx = new Wxtool();
        if (empty($param['code'])) {
            $this->error("参数有误");
        }
        $code = $param['code'];
        $res = $wx->GetSessionKey($code);
        $this->success('登录成功', $res);
    }

    public function getphone(userModel $user){
        $param = $this->request->param();
        $wx = new Wxtool();
        if (empty($param['session_key']) || empty($param['encryptedData']) || empty($param['iv']) || empty($param['openid'])) {
            $this->error("参数有误");
        }
        $encryptedData = urldecode($param['encryptedData']);
        $iv = urldecode($param['iv']);
        $userinfo = $wx->GetUserInfo($encryptedData, $iv,$param['session_key']);
        $userinfo = json_decode($userinfo, true);
        $openId = $param['openid'];
        $phone = empty($userinfo['phoneNumber']) ? '' : $userinfo['phoneNumber'];
        //'unionId' => $unionId,
        //$unionId = empty($param['unionid']) ? '' : $param['unionid'];
        $isExit = $user->where('wx_openid', $openId)->find();
        $saveData = [ "nickname" => $param['nickName'], 'update_time' => time(), 'gender' => $param['gender'], 'avatar' => $param['avatarUrl'],'phone' => $phone];
        if (!empty($isExit)) {
            $uid = $isExit['id'];
            $ret = $user->where('id', $uid)->update($saveData);
            
        }else {
            $user = new userModel();
            $saveData['wx_openid'] = $openId;
            $ret = $user->save($saveData);
             
            $uid = $user->id;
        }
        // file_put_contents('wx.log', "ret:" . $ret . PHP_EOL, FILE_APPEND);
        if ($ret) {
            $token = Auth::makeToken();
            $userToken = new Token();
            $userToken->setToken($token, $uid, $this->method);
            $user_info = ['nickname' => $param['nickName'], 'phone' => $phone,'avatar' => $param['avatarUrl']];
            $this->success('登录成功', ['token' => $token, 'method' => $this->method, 'user_info' => $user_info]);
        } else {
            $this->error('登陆失败，请稍后重试1');
        }
       
    }




     


}
